package cn.maple.general.single;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

public class MessageProducer {


    public static void main(String[] args) throws Exception {
        //单向消息
        singleMessage();
    }
    /**
     *singleMessage
     * 单向消息
     * @throws
     */
    public static void singleMessage() throws Exception {
        // 实例化消息生产者Producer
        DefaultMQProducer producer = new DefaultMQProducer("Single-message-Producer"/*生产者组名*/);
        // 设置NameServer的地址
        producer.setNamesrvAddr("localhost:9876");
        // 启动Producer实例
        producer.start();

        // 创建消息，并指定Topic，Tag和消息体
        Message msg = new Message("SingleMessage10086" /* Topic (组名)*/,
                "SingleTest1" /* Tag */,
                ("RocketMQ单向消息发送1========").getBytes(RemotingHelper.DEFAULT_CHARSET) /* Message body */
        );
        System.out.println("=====单向消息信息1=====");
        System.out.println(msg);

        //方法sendOneway发送单向消息
        producer.sendOneway(msg);
        /**
         发送消息到一个Broker
        //SendResult sendResult = producer.send(msg);
        // 通过sendResult返回消息是否成功送达
        System.out.printf(String.valueOf(sendResult));
         */

        // 如果不再发送消息，关闭Producer实例。
        producer.shutdown();

    }
}

